import {arrayToObject} from '../../../../assets/js/util.js' 
export default{
    data() {
      return{
		title:'实时充值流水',
		xlsxTitle:'',
		loading:false,
		// 默认参数 和 URL
		SRC: '',
		DP: {
			p: '',
			f: ''
		},
		searchable:false,
		pickerOptions: {
          	shortcuts: [{
	            text: '最近一周',
		            onClick(picker) {
		              const end = new Date();
		              const start = new Date();
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
		              picker.$emit('pick', [start, end]);
		            }
	          	}, {
	            text: '最近15天',
		            onClick(picker) {
		              const end = new Date();
		              const start = new Date();
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 15);
		              picker.$emit('pick', [start, end]);
	            }
	          	}, {
		            text: '最近一个月',
		            onClick(picker) {
		              const end = new Date();
		              const start = new Date();
		              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
		              picker.$emit('pick', [start, end]);
		            }
          	}]
        },
		dialogFormVisible: false,
		form: {
	  		date:'',

		  	dateStart: '',
		  	dateEnd:'',
			orderid:'',
			orderid2:'',
			userid:'',
			roleid:'',
			platform:'0',
			platforms:[],
			channel:'0',
			channels:[],
			serverid:'0',
			serverids:[],
			flag:'',
			addtype:'',
			addtypes:[],
			productid:'', 
			productids:[], 

			firstRecharges:[
				{label:'全部',value:'-1'},
				{label:'是',value:'1'},
				{label:'否',value:'0'}
			],
		},
		inputWidth:'220px',
	  	formLabelWidth: '100px',
      	/*当前页显示数量*/
      	pageSize:10,
      	/*每页显示的数量*/
      	pageSizes:[10, 20, 50, 100],
      	/*dataTotal表数量*/
      	dataTotal:0,
      	/*表格数据*/
      	realTimeServiceTableData:[],
      	/*一次取出所有数据*/
      	realTimeServiceTableDatas:[],
      	/*realTimeServiceContent 的宽*/
      	realTimeServiceContent:'100%',
      	/*表的宽度*/
      	realTimeServiceTableWidth: "100%",
      	/*数据表头*/
      	realTimeServiceTableHead:[
				{prop:'0',label:'内部订单号',width:'200'},
				{prop:'1',label:'外部订单号',width:'150'},
				{prop:'2',label:'详细时间',width:'160'},
				{prop:'3',label:'平台',width:'80'},
				{prop:'4',label:'渠道',width:'120'},
				{prop:'5',label:'区服',width:'120'},
				{prop:'6',label:'设备mac地址',width:'360'},
				{prop:'7',label:'账号ID',width:'140'},
				{prop:'8',label:'角色ID',width:'120'},
				{prop:'9',label:'角色等级',width:'80'},
				{prop:'10',label:'VIP等级',width:'80'},
				{prop:'11',label:'充值总金额',width:'120'},
				{prop:'12',label:'充值金额(￥)',width:'120'},
				{prop:'13',label:'本次增加元宝',width:'140'},
				{prop:'14',label:'是否首次付费',width:'120'},
				{prop:'15',label:'充值类型',width:'120'},
				{prop:'16',label:'商品名称',width:'80'}
	  		],
	    /*分页器*/
	    currentPage: 1,
    	rules: {
		    platform: [
		          { required: true, message: '请选择平台', trigger: 'change' }
		        ],
		    channel: [
		          { required: true, message: '请选择渠道', trigger: 'change' }
		        ],
		    serverid: [
		          { required: true, message: '请选择区服', trigger: 'change' }
		        ],
		    flag: [
		          { required: true, message: '请选择是否首充', trigger: 'change' }
		        ],
		    addtype: [
		          { required: true, message: '请选择支付方式', trigger: 'change' }
		        ],
		    productid: [
		          { required: true, message: '请选择商品名称', trigger: 'change' }
		        ]
			}
	    }
	},
	computed:{
		// xlsx(){
		// 	return this.title+'.xlsx'
		// }
	},
    methods: {
    	successFun(data) {
	      this.$message({
	        message: data,
	        type: 'success'
	      });
	    },
    	errorFun(data) {
	      this.$message.error(data);
	    },
    	dateChange(val){
    		var t = val.split(' - ')
    		this.form.dateStart = t[0];
    		this.form.dateEnd = t[1];
    	},
    	// 平台切换 渠道切换 区服切换
		selectPlats(val) {
			this.reGetData('updatePlatform',`platformid=${val}`)
		},
		selectChannels(val) {
			this.reGetData('updateChannel',`channelid=${val}`)
		},
		selectServers(val) {
			this.reGetData('updateServer',`serverid=${val}`)
		},
    	loads(){
    		this.loading=!this.loading;	//加载中
			this.searchable = !this.searchable;//查询按钮不可用
    	},
    	reset(form) {
	    	this.$refs[form].resetFields();
	    },
	    search(form){
			this.$refs[form].validate((valid) => {
	        if (valid) {
	         	this.commitData()
	        } else {
	          this.errorFun('请完善输入的值！');
	          return false;
	        }
	      })
		},
		commitData(){
			this.realTimeServiceTableDatas=[];
			this.realTimeServiceTableData=[];
			let obj={
				dateStart:this.form.dateStart,
				dateEnd:this.form.dateEnd,
				orderid:this.form.orderid,
				orderid2:this.form.orderid2,
				userid:this.form.userid,
				roleid:this.form.roleid,
				platform:this.form.platform,
				channel:this.form.channel,
				serverid:this.form.serverid,
				flag:this.form.flag,
				addtype:this.form.addtype,
				productid:this.form.productid
			}
			obj = encodeURI(JSON.stringify(obj));
			this.loads(); //加载 查询按钮不可用search
			this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=search`,{
				params:{'ps':'params='+obj},
			}).then(function(res){
				this.loads();
    			var data =  JSON.parse(JSON.parse(res.body))
    			if(parseInt(data["res"]) === 0){
  					if(data.data.length==0){
    					this.errorFun('查询结果为空！')
    				}else{
  						this.realTimeServiceTableDatas =arrayToObject(this.realTimeServiceTableHead,data.data);
  					}
	  			}else{
	  				this.errorFun(data["msg"])
	  			}
	  			this.dataTotal = this.realTimeServiceTableDatas.length;
    			this.realTimeServiceTableData = this.realTimeServiceTableDatas.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
    		})
		},
    	/*分页器*/
		handleSizeChange(val) {
			this.pageSize = val;
			// this.getData(this.pageSize,this.currentPage)
			this.realTimeServiceTableData = this.realTimeServiceTableDatas.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
		},
		handleCurrentChange(val) {
			this.currentPage = val;
			// this.getData(this.pageSize,this.currentPage)
			this.realTimeServiceTableData = this.realTimeServiceTableDatas.slice((this.currentPage-1)*this.pageSize,this.currentPage*this.pageSize)
		},
    	/*获取数据*/
    	getData: function(){
    		// initParam
    		this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=initParam`,{
				params:{'ps': '' }
				}).then(function(res){
    			var data =  JSON.parse(JSON.parse(res.body))
    			if(data.res == 0){
    				this._dataPush(data.channelItmes,this.form.channels)
	    			this._dataPush(data.serverItmes,this.form.serverids)
	    			this._dataPush(data.platformItmes,this.form.platforms)
	    			this._dataPush(data.productItmes,this.form.productids)
	    			this._dataPush(data.addtypeItmes,this.form.addtypes)
    			}else{
    				this.getErrorMsg(Number(data.res))
    			}
    			// this.form.channel = {label:this.form.channels[0]['label'],value:this.form.channels[0]['label']}
    		})
    	},
    	/*获取数据*/
    	reGetData: function(url){
    		this.$http.jsonp(`${this.SRC}data/get.do?p=${this.DP.p}&f=${this.DP.f}&a=${url}`,{
				params:{'ps': '' }
			}).then(function(res){
    			var data =  JSON.parse(JSON.parse(res.body))
    			if(data.res == 0){
    				console.log(data)
    				if(url.indexOf('serverItmes')!=-1){
    					this.form.channels=[]
    					this.form.platforms=[]
	    				this._dataPush(data.channelItmes,this.form.channels)
		    			this._dataPush(data.platformItmes,this.form.platforms)
    				}else if(url.indexOf('platformItmes')!=-1){
    					this.form.channels=[]
    					this.form.serverids=[]
    					this._dataPush(data.channelItmes,this.form.channels)
	    				this._dataPush(data.serverItmes,this.form.serverids)
    				}else if(url.indexOf('channelItmes')!=-1){
    					this.form.serverids=[]
    					this.form.platforms=[]
		    			this._dataPush(data.serverItmes,this.form.serverids)
		    			this._dataPush(data.platformItmes,this.form.platforms)
    				}
    			}else{
    				this.errorFun(data.msg)
    			}
    			// this.form.channel = {label:this.form.channels[0]['label'],value:this.form.channels[0]['label']}
    		})
    	},
    	_dataPush(firstData,lastData){
			for (var key in firstData){
		        lastData.push({label:key,value:firstData[key]})
			}
    	},
    	/*设置初始值*/
    	_init(){
    		window.addEventListener('resize', function() {
    			/*修改表格的宽度*/
          		this.realTimeServiceTableWidth = document.body.clientWidth-240+"px";
          		this.realTimeServiceContent = document.body.clientWidth-220+"px";
        	}.bind(this))
    	},
	  	// 表格下载
	  	excel(val) {
			this.xlsxTitle = val+"-"+this.form.dateStart+"到"+this.form.dateEnd;
			this.downloadExl(this.realTimeServiceTableHead,this.realTimeServiceTableDatas,this.xlsxTitle,this.title);
	  	},
    	/*选择平台切换*/
		head_choose_plat_fun(index) {
			for(var i=0; i<this.head_choose_plat.length; i++){
				this.head_choose_plat[i].activeIndex = false;
			}
			this.head_choose_plat[index].activeIndex = true;
		},
		head_choose_time_fun(index) {
			for(var i=0; i<this.head_choose_time.length; i++){
				this.head_choose_time[i].activeIndex = false;
			}
			this.head_choose_time[index].activeIndex = true;
		},
    },
    mounted() {
     	// 初始化URL
		this.SRC = this.$store.getters.src;
		this.DP.p = localStorage.getItem('ZULONGBIPROJECTID');
		this.DP.f = localStorage.getItem('ZULONGBIPAGEID');
     	this._init()
     	this.getData()
     	this.form.date = [new Date(),new Date()];
     	// this.getData(this.pageSize,this.currentPage)
    }
}